package com.xbdun.service.bmp.facade.open;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.facade.bmp.request.open.CreateUserByInviteCodeRequest;
import com.xbdun.facade.bmp.request.open.OrgUserBkgeRequest;
import com.xbdun.facade.bmp.request.open.UserInfoBmpRequest;
import com.xbdun.facade.bmp.request.system.SysUserRequest;
import com.xbdun.facade.bmp.response.open.OrgUserBkgeResponse;
import com.xbdun.facade.bmp.response.open.OrgUserSaoInfoWayResponse;
import com.xbdun.facade.bmp.response.open.UserInfoBmpResponse;
import com.xbdun.facade.bmp.response.system.SysUserResponse;
import com.xbdun.facade.bmp.result.user.CreateUserByInviteCodeResult;
import com.xbdun.facade.bmp.service.open.UserInfoFacade;
import com.xbdun.service.bmp.service.system.OrgUserService;
import com.xbdun.service.bmp.service.system.SysUserService;

import lombok.extern.slf4j.Slf4j;

/**
* @ClassName: UserInfoFacadeImpl
* @Description: 获取用户基本信息
* @author zhanggt 360730440@qq.com
* @date 2017年5月22日 下午5:52:37
*/
@Component
@Slf4j
public class UserInfoFacadeImpl implements UserInfoFacade{
	
	@Autowired
	SysUserService sysUserService;
	@Autowired
	OrgUserService orgUserService;
	public UserInfoBmpResponse getUserInfo(UserInfoBmpRequest reqeust) {
		UserInfoBmpResponse result = null;
		SysUserRequest sysUser = null;
		try{
			//获取用户基本信息
			sysUser = new SysUserRequest();
			sysUser.setUnionid(reqeust.getWxUnionId());
			sysUser.setUserCode(reqeust.getUserCode());
			SysUserResponse sysUserLogin = sysUserService.getSysUserLogin(sysUser);
			//获取用户信息
			if(null!=sysUserLogin){
				result = new UserInfoBmpResponse();
				result.setImgUrl(sysUserLogin.getImgUrl());
				result.setSex(sysUserLogin.getSex());
				result.setUserCode(sysUserLogin.getUserCode()+"");
				result.setUnionId(sysUserLogin.getUnionid());
				result.setUserName(StringUtils.defaultIfBlank(sysUserLogin.getRealName(),sysUserLogin.getUserName()));
			}
		}catch(Exception ex){
			log.error("获取用户基本信息,入参:{},异常:{}",JSON.toJSONString(reqeust),ex);
		}finally{
			sysUser = null;
		}
		return result;
	}
	
	public CreateUserByInviteCodeResult createUserByInviteCode(CreateUserByInviteCodeRequest reqeust) {
		CreateUserByInviteCodeResult result = null;
		try{
			result = orgUserService.createUserByInviteCode(reqeust);
		}catch(Exception e){
			log.error("根据邀请码创建用户关系，入参:{}",JSON.toJSONString(reqeust),e);
			result = new CreateUserByInviteCodeResult();
			result.setBizException(BizException.SYS_BUSY_0);
		}
		return result;
	}

	public Long getUserCodeByUnionId(String unionId) {
		try{
			return sysUserService.getUserCodeByUnionId(unionId);
		}catch(Exception e){}
		return null;
	}

	public OrgUserSaoInfoWayResponse getOrgUserSaoInfo(String orgCode, String userCode) {
		try{
			//获取扫码得到机构用户信息
			return sysUserService.getOrgUserSaoInfo(NumberUtils.toLong(orgCode),NumberUtils.toLong(userCode));
		}catch(Exception e){
			log.error("获取机构用户信息异常",e);
		}
		return null;
	}

	public OrgUserBkgeResponse getOrgUserBkge(OrgUserBkgeRequest request) {
		OrgUserBkgeResponse response = null;
		try{
			if(null==request.getOrgCode()||null==request.getUserCode()){return response;}
			
			response = orgUserService.getOrgUserBkge(request.getOrgCode(),request.getUserCode());
		}catch(Exception ex){
			log.error("根据机构用户编号获取机构用户的角色，状态,机构["+request.getOrgCode()+"],用户编号["+request.getUserCode()+"]",ex);
		}
		return response;
	}
}
